// pages/newcard/newcard.js
const app = getApp();
var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: 'fixed', value: '固定期限', checked: 'true' },
      { name: 'after', value: '自领取后' },
      { name: 'after2', value: '自领取次日起' },
    ],
    date: '',
    endDate: '',
    dateTime: "",
    dateDay: '',
    index: '',
    couponType: [],//优惠券类型
    coupon_type_id: '',//类型id
    ishidden: false,
    name: "",//名字
    coupon_desc: '',//描述
    price: '',//价格
    min_payment: '',
    stock: '',
    time_limit_type: 1,
    start_time: '',
    end_time: '',
    days: '',
    next_days: '',
    data:{}
  },
  shows(msg) {
    wx.showToast({
      title: msg,
    })
  },
  // 验证列表
  formSubmit(e) {
    let form = e.detail.value;
    let that = this;
    console.log(that.data.time_limit_type)
    if (!form.name) {
      that.shows("请输入卡券名字")
      return
    }

    if (!form.price) {
      that.shows("请输入卡券价格")
      return
    }
    if (!form.min_payment) {
      that.shows("请输入卡券最低价格")
      return
    }
    if (!form.stock) {
      that.shows("请输入卡券库存数量")
      return
    }

    if (form.time_limit_type == 1) {
      if (!that.data.date & !that.data.endDate) {
        that.shows("请选择时间")
        return
      }
      that.setData({ days: '', next_days: '' })
    } else if (form.time_limit_type == 2) {
      if (!form.days) {
        that.shows("请输入领取后有效时间")
        return
      }
      that.setData({ date: null, endDate: null })
    } else {
      if (!form.next_days) {
        that.shows("请输入领取后次日有效时间")
        return
      }
      that.setData({ date: null, endDate: null, days: '' })
    }
    let postData = {
      id: that.data.data.id,
      coupon_type_id: that.data.coupon_type_id == "" ? that.data.data.coupon_type_id : that.data.coupon_type_id,
      start_time: that.data.date,
      end_time: that.data.endDate,
      ...form
    }
    console.log(postData)
    app.post("/coupon/edit", "put", postData).then(res => {
      that.shows(res.msg);
      setTimeout(function () {
        wx.navigateTo({
          url: '../manageCardAll/manageCardAll'
        })
      }, 500)


    })
  },
  //单选按钮
  radioChange(e) {
    let that = this;
    that.setData({ time_limit_type: e.detail.value })
  },
  changeDate(e) { //开始时间
    this.setData({ date: e.detail.value });
  },
  changeEndDate(e) {//结束时间
    this.setData({ endDate: e.detail.value });
  },
  // 卡券名称
  couponName(e) {
    let that = this;
    that.setData({ name: e.detail.value })
  },
  couponDsoc(e) {
    let that = this;
    that.setData({ coupon_desc: e.detail.value })
  },
  couponPrice(e) {
    let that = this;
    that.setData({ price: e.detail.value })
  },
  couponMinPrice(e) {
    let that = this;
    that.setData({ min_payment: e.detail.value })
  },
  couponStock(e) {
    let that = this;
    that.setData({ stock: e.detail.value })
  },
  couponDays(e) {
    let that = this;
    that.setData({ days: e.detail.value })
  },
  couponNextDays(e) {
    let that = this;
    that.setData({ next_days: e.detail.value })
  },
  //选择类型
  bindPickerChange(e) {
    console.log(e)
    let that = this;
    that.setData({
      index: e.detail.value,
      coupon_type_id: that.data.couponType[e.detail.value].id,
      ishidden: true
    })
  },
  right: function () {
    var that = this
    wx.navigateTo({
      url: '../CardType/CardType'
    })
  },
  confirm: function () {
    var that = this;
    that.addCoupon(0)

  },
  tf: function () {
    var that = this;
    that.addCoupon()
    // wx.navigateTo({
    //   url: '../manageCardAll/manageCardAll'
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
   
    var data = JSON.parse(options.data)
    that.setData({ data: data, time_limit_type: data.time_limit_type});
    if (data.time_limit_type==1){
      that.setData({ date: data.start_time, endDate: data.end_time });
    } else if (data.time_limit_type == 2){
      that.setData({ days: data.days });
    }else{
      that.setData({ next_days: data.next_days });
    }

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    var myDate = new Date();//获取系统当前时间
    var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var day = myDate.getDate(); //获取当前日(1-31)
    var dateDay = year + "-" + month + "-" + day
    this.setData({
      dateTime: obj.dateTime,
      dateDay: dateDay,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    app.post("/category/coupon_type", "get", {}).then(res => {
      that.setData({ couponType: res })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})